<template>
  <div class="biaoqi">
    <el-dialog :title="title" :visible.sync="isshownow"  width="560px">
      <div v-if="nowtype==1&&!isshowinfo">
        <div class="kflexc t_item_info">
          <div class="yzh_title">公司主体</div><el-input v-model="upform.name" placeholder="请输入"  style="width: 360px; margin-right: 20px;"></el-input>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">平台企业ID</div><el-input v-model="upform.dealer_id" placeholder="请输入"  style="width: 360px; margin-right: 20px;"></el-input>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">综合服务主体ID</div><el-input  v-model="upform.broker_id" placeholder="请输入"  style="width: 360px; margin-right: 20px;"></el-input>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">App Key</div><el-input placeholder="请输入" v-model="upform.app_key" style="width: 360px; margin-right: 20px;"></el-input>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">3DES key</div><el-input placeholder="请输入" v-model="upform.des_key" style="width: 360px; margin-right: 20px;"></el-input>
        </div>
        <div style="color: rgb(126, 126, 126); font-size: 14px; padding-left: 140px;">
          <div style="margin-bottom: 20px;">测试打款：</div>
          <div>系统将会驱动您的账户向我们的固定账户打款一分钱用来测试，请确保账 户中有余额，该测试打款不会退回。</div>
        </div>
      </div>

      <!-- 查看 -->
      <div v-if="nowtype==2||isshowinfo">
        <div class="kflexc t_item_info">
          <div class="yzh_title">公司主体：</div>
          <div>{{ upform.name }}</div>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">平台企业ID：</div>
          <div>{{ upform.dealer_id }}</div>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">综合服务主体ID：</div>
          <div>{{ upform.broker_id }}</div>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">App Key：</div>
          <div>{{ upform.app_key }}</div>
        </div>
        <div class="kflexc t_item_info">
          <div class="yzh_title">3DES key：</div>
          <div>{{ upform.des_key }}</div>
        </div>
      </div>

      <div v-if="nowtype==3||isshowinfo">
        <div class="kflexc t_item_info">
          <div class="yzh_title">公司主体：</div>
          <el-input v-model="all.name" placeholder="请输入"  style="width: 360px; margin-right: 20px;"></el-input>
        </div>
      </div>




      <div style="text-align: center;margin-top: 20px;">
        <el-button round  @click="isshownow = false">取消</el-button>
        <el-button round type="danger" v-if="nowtype==1" @click="opentest">测试打款并绑定</el-button>
        <el-button round type="danger" v-if="nowtype==3" @click="xiugai">确定</el-button>
      </div>
    </el-dialog>

    <!-- 绑定成功 -->
    <el-dialog
      title="绑定成功"
      :visible.sync="isshowbindsuccess"
      :close-on-click-modal="false"
      width="440px">
      <div style="color: #AFB3BC;font-size: 14px;">云账户已成功绑定！</div>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="isshowbindsuccess = false">取 消</el-button>
        <el-button round type="primary" @click="successclose">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 绑定失败 -->
    <el-dialog
      title="绑定失败"
      :visible.sync="isshowbindfail"
      :close-on-click-modal="false"
      width="440px">
      <div style="color: #AFB3BC;font-size: 14px;">{{ failmsg }}</div>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="isshowbindfail = false">取 消</el-button>
        <el-button round type="primary" @click="isshowbindfail = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 绑定测试 -->
    <el-dialog
      title="绑定测试"
      :visible.sync="isshowbindtest"
      :close-on-click-modal="false"
      width="440px">
      <div style="color: #AFB3BC;font-size: 14px;">我们将会使用您的云账户打款1分钱到我们的个人账户，用来测试是否绑定成功，1分钱不会退款。请先确保云账户余额大于1分钱再点击测试</div>
      <span slot="footer" class="dialog-footer">
        <el-button round @click="isshowbindtest = false">取 消</el-button>
        <el-button v-if="!isnowtest" round type="primary" @click="baocunpeizhi">我同意，开始测试</el-button>
        <el-button v-else type="primary" round :loading="isnowtest">测试中</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from "axios";
  import chuantu from "@/components/zujian/chuantu.vue";
  export default {
    components:{
      chuantu
    },
    props:['nowtype','now'],
    name: "yunzhanghubangding",
    data() {
      return {
        isshownow:true,
        isshowbindsuccess:false,
        isshowbindfail:false,
        // nowtype:'',//1注册，2查看
        isshowbindtest:false,
        isnowtest:false,
        all:{
          id:"",
          name:""
        },
        upform:{
          name:"",
          dealer_id:'',
          broker_id:'',
          app_key:'',
          des_key:'',
          // dealer_pub:'',
          // yun_pub:''
        },
        user:"",
        failmsg:'',
        isshowinfo:false,
        title:"云账户绑定"
      }
    },
    created() {

      if(this.nowtype==2 || this.nowtype==3){
        this.upform = this.now
        this.all.id = this.now.id
        this.all.name = this.now.name
        //查看
        // this.getinfo()
      }
      if(this.nowtype==3){
        this.title = '云账户修改'
      }
    },
    mounted() {

    },
    methods: {
      xiugai:function(){
        axios.post('/api/plan-market/yunzhifu/editYunAccount',{id:this.all.id,name:this.all.name}).then((response)=>{
          if (response.data.msg.code == 0) {
            this.isshownow = false
            this.$emit('bangdingok')
          } else {
            this.$message.error(response.data.msg.info);
          }
        })
      },
      //打开测试
      opentest(){
          if(
            this.upform.name==''||
            this.upform.dealer_id==''||
            this.upform.broker_id==''||
            this.upform.app_key==''||
            this.upform.des_key==''
          ){
            this.$message.error('信息未填写完整');
            return
          }

          let obj = {
            name:this.upform.name,
            dealer_id:this.upform.dealer_id,
            broker_id:this.upform.broker_id,
            app_key:this.upform.app_key,
            des_key:this.upform.des_key,
          }

          axios.post('/api/plan-market/yunzhifu/testApiToken',obj).then((response)=>{
            if (response.data.msg.code == 0) {
              this.isnowtest=false
              this.isshowbindsuccess = true
              this.isshowinfo = true

              axios.post('/api/plan-market/yunzhifu/bandApiToken',this.upform).then((response)=>{
                if (response.data.msg.code == 0) {
                  // this.$message({
                  //   message: '信息已保存',
                  //   type: 'success'
                  // });
                  this.isshownow = false
                  this.$emit('bangdingok')
                } else {
                  this.$message.error(response.data.msg.info);
                }
              })


            } else {
              this.isnowtest=false
              this.isshowbindfail = true
              this.failmsg = response.data.msg.info
              this.$message.error(response.data.msg.info);
            }
          })

        // axios.post('/api/plan-market/yunzhifu/bandApiToken',this.upform).then((response)=>{
        //   if (response.data.msg.code == 0) {
        //     this.$message({
        //       message: '信息已保存',
        //       type: 'success'
        //     });
        //     this.isshowbindtest=true
        //     this.isnowtest=false

        //   } else {
        //     this.$message.error(response.data.msg.info);
        //   }
        // })

      },
      //保存
      baocunpeizhi(){
        this.isnowtest=true
        // axios.post('/api/plan-market/yunzhifu/testApiToken',{dealer_id:this.upform.dealer_id}).then((response)=>{
        //   if (response.data.msg.code == 0) {
        //     this.isnowtest=false
        //     this.isshowbindsuccess = true
        //     this.isshowinfo = true

        //     this.$emit('bangdingok')
        //   } else {
        //     this.isnowtest=false
        //     this.isshowbindfail = true
        //     this.failmsg = response.data.msg.info
        //     this.$message.error(response.data.msg.info);
        //   }
        // })

      } ,
      //确定成功
      successclose(){
        this.isshowbindsuccess = false
        this.isshowbindtest = false
      },
      //获取详情
      // getinfo(){
      //   axios.get('/api/plan-market/yunzhifu/getApiToken').then((response)=>{
      //     if (response.data.msg.code == 0) {
      //       this.upform = response.data.data
      //     } else {
      //       this.$message.error(response.data.msg.info);
      //     }
      //   })
      // }

    }
  }
</script>

<style lang="scss" scoped>
  .yzh_title{ width: 120px; margin-right: 20px; text-align: right;}
  .t_item_info{ margin-bottom: 20px;}
</style>
